// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// C [author] rhyme_lph
// T [email]  rhymelph@gmail.com
// y [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/utils/language_utils.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/list_item/list_header_item.dart';
import 'package:finance_app/src/widgets/list_item/setting_list_item.dart';
import 'package:finance_app/src/widgets/select/normal_check_box.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/setting/preferences_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';

@RouterMeta(paramName: "preferencesPage")
class PreferencesPage extends StatefulWidget {
  @override
  _PreferencesPageState createState() => _PreferencesPageState();
}

class _PreferencesPageState extends State<PreferencesPage>
    with PagePresenter<PreferencesPage, PreferencesProvider> {
  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
        title: S.current.preferences,
      );

  @override
  Widget get buildWidget => ColoredBox(
        color: Colors.white,
        child: ListView(
          children: [
            ListHeaderItem(
              title: S.current.default_settings,
            ),
            rootListBuilder<String>(
              selector: (_, __) => Config.languageCode,
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                title: S.current.language,
                onTap: provider.onChangeLanguage,
                value: LanguageUtils.getLanguageName(value),
              ),
            ),
            rootListBuilder<String>(
              selector: (_, __) => Config.settingInfo?.currencyCode?.name,
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                title: S.current.currency,
                onTap: provider.onChangeCurrency,
                value: value,
              ),
            ),
            rootListBuilder<String>(
              selector: (_, __) => Config.settingInfo?.paymentType?.name,
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                title: S.current.payment_method,
                onTap: provider.onChangePaymentType,
                value: value,
              ),
            ),
            rootListBuilder<String>(
              selector: (_, __) => Config.paymentTerm?.name,
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                title: S.current.payment_term,
                onTap: provider.onChangePaymentTerm,
                value: value,
              ),
            ),
            rootListBuilder<String>(
              selector: (_, __) => Config.settingInfo?.taxData?.displayName,
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                title: S.current.tax_type,
                onTap: provider.onChangeTaxType,
                value: value,
                border: null,
              ),
            ),
            ListHeaderItem(
              title: S.current.format_style,
            ),
            rootListBuilder<String>(
              selector: (_, __) => Config.settingInfo?.dateFormat,
              builder: (BuildContext context, String value, Widget child) =>
                  ValueSelectForm(
                title: S.current.date_format,
                onTap: provider.onChangeDateFormat,
                value: value.toUpperCase(),
              ),
            ),
            SettingListItem(
              title: S.current.number_format,
              onTap: provider.onNumberFormat,
            ),
            SettingListItem(
              title: S.current.template_style,
              onTap: provider.onTemplateStyle,
              border: null,
            ),
            ListHeaderItem(
              title: S.current.custom_fields,
            ),
            SettingListItem(
              title: S.current.unit_of_measurement,
              onTap: provider.onUnitOfMeasurement,
              border: null,
            ),
            ListHeaderItem(
              title: S.current.alert_settings,
            ),
            ListHeaderItem(
              hasBackgroundColor: false,
              title: S.current.send_notification_when,
            ),
            rootListBuilder<List<String>>(
              selector: (_, __) => List.from(Config.settingInfo.alerts),
              builder:
                  (BuildContext context, List<String> value, Widget child) =>
                      Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: Config.settingData.alerts
                    .map((e) => PressedOpacityButton(
                          onPressed: () => provider.onCheckAlertSetting(e),
                          child: NormalFormWrapper(
                            bottomPadding: getDp(10),
                            topPadding: getDp(10),
                            child: listBuilder<bool>(
                              selector: (_, __) => value.contains(e.key),
                              builder: (BuildContext context, bool value,
                                      Widget child) =>
                                  NormalCheckBox(
                                title: e.name,
                                isSelect: value,
                              ),
                            ),
                          ),
                        ))
                    .toList(),
              ),
            ),
            AppSpaces.h30,
          ],
        ),
      );
}
